package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.constant.MessageConstant;
import com.itcast.constant.RedisConstant;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.entity.Result;
import com.itcast.pojo.Setmeal;
import com.itcast.service.SetmealService;
import com.itcast.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
@Reference
private SetmealService setmealService;

@Autowired
private JedisPool jedisPool;
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        try {
        String file= UUID.randomUUID()+".jsp";
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),file);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,file);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,file);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
//        用户登录名称 13061234263.onaliyun.com@1330190572316118.onaliyun.com
//登录密码 0dQ7kLMLum9{5$RUM?#TOyzZelcZhblH
//AccessKey ID LTAI4G5ihX4uHcnr9mX3pExL
//AccessKey Secret RO9gGaw5WHj6XK0JVwlTS4dJGkvwCv
    }
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @RequestMapping("/add")
    public Result findAllCheckGroup(@RequestBody Setmeal setmeal ,Integer[] checkgroupIds){
        try {
            Integer id = setmealService.add(setmeal,checkgroupIds);

            setmealService.genneratSetmealHtml(id);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }

    }

    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }
}
